from django.shortcuts import render
from django.shortcuts import HttpResponse
from django.views import View
import random
from django.conf import settings
from utils.tencent.sms import send_sms_single
from app01.forms.userinfo import UserInfoModelForm
from django_redis import get_redis_connection

def send_sms(request):
    tpl=request.GET.get('tpl')
    template_id=settings.TENCENT_SMS_TEMPLATE.get(tpl)
    if not template_id:
        return HttpResponse('模板不存在')
    code=random.randrange(1000,9999)
    res=send_sms_single('13717565305',template_id,[code,])
    if res['result']==0:
        print('ok')
    else:
        print(res['errmsg'])
    return HttpResponse(code)

class RegisterView(View):

    def get(self,request):
        #测试redis
        conn=get_redis_connection('default')
        conn.set('nickname','sakula',ex=10)
        value=conn.get('nickname')
        print(value)
        form=UserInfoModelForm()
        return render(request,'register.html',{'form':form})

    def post(self,request):

        form=UserInfoModelForm(data=request.POST)
        if form.is_valid():
            return HttpResponse('注册成功')
        return render(request,'register.html',{'form':form})